package com.globallogic.dailyplanner.service.impl;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.globallogic.dailyplanner.dto.DailyPlan;
import com.globallogic.dailyplanner.repository.DailyPlanRepository;
import com.globallogic.dailyplanner.service.DailyPlanService;

@Service
public class DailyPlanServiceImpl implements DailyPlanService {

	@Autowired
	private DailyPlanRepository dpRepository;
	
	@Override
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public List<DailyPlan> getAllById(int id) {
		return dpRepository.findById(id);
	}

	@Override
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public DailyPlan getById(int id) {
		return dpRepository.getOne(id);
	}

	@Override 
	@Transactional(propagation=Propagation.REQUIRES_NEW, timeout=30)
	public DailyPlan save(DailyPlan dailyPlan) {
		return dpRepository.saveAndFlush(dailyPlan);
	}

	@Override 
	@Transactional(propagation=Propagation.REQUIRES_NEW, timeout=30)
	public void delete(int id) {
		dpRepository.delete(id);
	}

	@Override
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public List<DailyPlan> getAll() {
		return dpRepository.findAll();
	}

	@Override
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public DailyPlan getOneByNameAndTime(String name, Date startDate,
			String estimateDuration) {
		return dpRepository.findByNameAndTime(name, startDate, estimateDuration);
	}

	@Override
	public int getCountOfDailyPlans() {
		return dpRepository.findAll().size();
	}

	@Override
	public List<DailyPlan> getAllByDate(Calendar startDate, Calendar endDate) {
		return dpRepository.findAllByDate(startDate, endDate);
	}
}
